/**
 * Created on [2022-11-11] by zhengrr
 *
 * 项目名称: btfvilg
 *
 * 本程序版权属于福建慧政通信息科技有限公司所有。
 *
 * 任何组织和个人未经福建慧政通信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 *
 * ©2021 福建慧政通信息科技有限公司
 */
package com.sunnada.btfvilg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunnada.btfvilg.entity.Address;
import com.sunnada.btfvilg.entity.Deliverylist;
import com.sunnada.btfvilg.entity.Package;
import com.sunnada.btfvilg.entity.User;
import com.sunnada.btfvilg.mapper.AddressMapper;
import com.sunnada.btfvilg.mapper.PackageMapper;
import com.sunnada.btfvilg.mapper.UserMapper;
import com.sunnada.btfvilg.model.request.PackageParamVo;
import com.sunnada.btfvilg.model.response.*;
import com.sunnada.btfvilg.service.AddressService;
import com.sunnada.btfvilg.service.PackageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunnada.btfvilg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 *  服务实现类
 *
 * @author zhengrr
 */
@Service
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package> implements PackageService {

    @Autowired(required = false)
    PackageMapper packageMapper;
    @Autowired(required = false)
    AddressMapper addressMapper;
    @Autowired(required = false)
    UserMapper userMapper;

    public List<PackageItemVo> ItemExtend(List<Package> packages){
        List<PackageItemVo> packagesItem = new ArrayList<>();
        for(Package p:packages){
            PackageItemVo pitem=new PackageItemVo();
            User user=userMapper.selectById(p.getUid());
            Address uaddress=addressMapper.selectById(p.getUaid());
            Address raddress=addressMapper.selectById(p.getRaid());
            //包裹基本信息
            pitem.setPid(p.getPid());
            pitem.setPstate(p.getPstate());
            pitem.setWeigh(p.getWeigh());
            //寄件人信息
            pitem.setUid(p.getUid());
            pitem.setUname(user.getUname());
            pitem.setUphone(user.getUphone());
            pitem.setUaid(p.getUaid());
            pitem.setUprovince(uaddress.getProvince());
            pitem.setUcounty(uaddress.getCounty());
            pitem.setUcity(uaddress.getCity());
            pitem.setUtown(uaddress.getTown());
            pitem.setUlocation(uaddress.getLocation());
            //收件人信息
            pitem.setRname(p.getRname());
            pitem.setRphone(p.getRphone());
            pitem.setRaid(p.getRaid());
            pitem.setRprovince(raddress.getProvince());
            pitem.setRcounty(raddress.getCounty());
            pitem.setRcity(raddress.getCity());
            pitem.setRtown(raddress.getTown());
            pitem.setRlocation(raddress.getLocation());
            packagesItem.add(pitem);
        }
        return packagesItem;
    }

    @Override
    public PackageVo addPackage(PackageParamVo packageVo) {
        Package p1 = new Package();
        p1.setPid(UUID.randomUUID().toString().replaceAll("-",""));
        p1.setRaid(packageVo.getRaid());
        p1.setUaid(packageVo.getUaid());
        p1.setWeigh(packageVo.getWeigh());
        p1.setRphone(packageVo.getRphone());
        p1.setPstate(packageVo.getPstate());
        p1.setUid(packageVo.getUid());
        p1.setRname(packageVo.getRname());
        packageMapper.insert(p1);
        PackageVo reg = new PackageVo(p1.getPid(),p1.getPstate());
        return reg;
    }

    @Override
    public void delPackage(String pid) {
        packageMapper.deleteById(pid);
    }

    @Override
    public void updatePackage(PackageParamVo packageVo) {
        Package p1=new Package();
        p1.setPid(packageVo.getPid());
        p1.setRaid(packageVo.getRaid());
        p1.setUaid(packageVo.getUaid());
        p1.setWeigh(packageVo.getWeigh());
        p1.setRphone(packageVo.getRphone());
        p1.setPstate(packageVo.getPstate());
        p1.setUid(packageVo.getUid());
        p1.setRname(packageVo.getRname());
        packageMapper.updateById(p1);
    }
    @Override
    public PackageListVo getPackageList( Integer page, Integer numPerPage) {
        PackageListVo packageList = new PackageListVo();
        QueryWrapper<Package> qw = new QueryWrapper<>();
        Integer count = packageMapper.selectCount(qw);
        packageList.setTotal(count);
        List<Package> packages = null;
        packages = packageMapper.getPackageList((page - 1) * numPerPage, numPerPage);
        List<PackageItemVo> packagesItem = ItemExtend(packages);
        packageList.setPackageList(packagesItem);
        packageList.setPage(page);
        packageList.setNumPerPage(numPerPage);
        return packageList;
    }

    @Override
    public PackageListVo getPackageListBySid(String sid, Integer page, Integer numPerPage) {
        List<Deliverylist> packages_id = null;
        //读取网点对应的包裹id
        packages_id = packageMapper.getPackageListBySid(sid,(page - 1) * numPerPage, numPerPage);
        List<Package> packages = new ArrayList<>();
        for(Deliverylist s:packages_id) {//从包裹列表中选出对应的包裹
            packages.add(packageMapper.selectById(s.getPid()));
        }
        List<PackageItemVo> packagesItem = ItemExtend(packages);
        PackageListVo packageList = new PackageListVo();
        QueryWrapper<Package> qw = new QueryWrapper<>();
        //Integer count = packageMapper.selectCount(qw);
        Integer count = packages.size();
        packageList.setTotal(count);
        packageList.setPackageList(packagesItem);
        packageList.setPage(page);
        packageList.setNumPerPage(numPerPage);
        return packageList;
    }

    @Override
    public PackageListVo getPackageListByUid(String uid, Integer page, Integer numPerPage) {
        PackageListVo packageList = new PackageListVo();
        QueryWrapper<Package> qw = new QueryWrapper<>();
        //Integer count = packageMapper.selectCount(qw);

        List<Package> packages = null;
        packages = packageMapper.getPackageListByUid(uid,(page - 1) * numPerPage, numPerPage);
        List<PackageItemVo> packagesItem = ItemExtend(packages);
        Integer count = packages.size();
        packageList.setTotal(count);
        packageList.setPackageList(packagesItem);
        packageList.setPage(page);
        packageList.setNumPerPage(numPerPage);
        return packageList;
    }

    @Override
    public PackageListVo getPackageListByDid(String did, Integer page, Integer numPerPage) {
        List<Deliverylist> packages_id = null;
        packages_id = packageMapper.getPackageListByDid(did,(page - 1) * numPerPage, numPerPage);
        List<Package> packages = new ArrayList<>();
        for(Deliverylist s:packages_id) {
            packages.add(packageMapper.selectById(s.getPid()));
        }
        List<PackageItemVo> packagesItem = ItemExtend(packages);
        PackageListVo packageList = new PackageListVo();
        QueryWrapper<Package> qw = new QueryWrapper<>();
        //Integer count = packageMapper.selectCount(qw);
        Integer count = packages.size();
        packageList.setTotal(count);
        packageList.setPackageList(packagesItem);
        packageList.setPage(page);
        packageList.setNumPerPage(numPerPage);
        return packageList;
    }

    @Override
    public PackageItemVo getPackageByPid(String pid) {
        Package p=packageMapper.selectById(pid);
        PackageItemVo pitem=new PackageItemVo();
        User user=userMapper.selectById(p.getUid());
        Address uaddress=addressMapper.selectById(p.getUaid());
        Address raddress=addressMapper.selectById(p.getRaid());
        //包裹基本信息
        pitem.setPid(p.getPid());
        pitem.setPstate(p.getPstate());
        pitem.setWeigh(p.getWeigh());
        //寄件人信息
        pitem.setUid(p.getUid());
        pitem.setUname(user.getUname());
        pitem.setUphone(user.getUphone());
        pitem.setUaid(p.getUaid());
        pitem.setUprovince(uaddress.getProvince());
        pitem.setUcounty(uaddress.getCounty());
        pitem.setUcity(uaddress.getCity());
        pitem.setUtown(uaddress.getTown());
        pitem.setUlocation(uaddress.getLocation());
        //收件人信息
        pitem.setRname(p.getRname());
        pitem.setRphone(p.getRphone());
        pitem.setRaid(p.getRaid());
        pitem.setRprovince(raddress.getProvince());
        pitem.setRcounty(raddress.getCounty());
        pitem.setRcity(raddress.getCity());
        pitem.setRtown(raddress.getTown());
        pitem.setRlocation(raddress.getLocation());
        return pitem;
    }

    @Override
    public void Packagestate(String pid, String pstate) {
        packageMapper.packageState(pid, pstate);
    }

}
